package com.zrj.auth.pojo.response;

import lombok.Data;

import java.io.Serializable;

/**
 * @author zhaorujie
 * @param <T>
 */
@Data
public class Results<T> implements Serializable {

    private String code;

    private T data;

    private String msg;

    public static <T> Results<T> success() {
        return success(null);
    }

    public static <T> Results<T> success(T data) {
        Results<T> results = new Results<>();
        results.setCode(ResultCode.SUCCESS.getCode());
        results.setMsg(ResultCode.SUCCESS.getMsg());
        results.setData(data);
        return results;
    }

    public static <T> Results<T> failed() {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMsg(), null);
    }

    public static <T> Results<T> failed(String msg) {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
    }

    public static <T> Results<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    public static <T> Results<T> failed(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null);
    }

    public static <T> Results<T> failed(IResultCode resultCode, String msg) {
        return result(resultCode.getCode(), msg, null);
    }

    private static <T> Results<T> result(IResultCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMsg(), data);
    }

    private static <T> Results<T> result(String code, String msg, T data) {
        Results<T> results = new Results<>();
        results.setCode(code);
        results.setData(data);
        results.setMsg(msg);
        return results;
    }

    public static boolean isSuccess(Results<?> results) {
        return results != null && ResultCode.SUCCESS.getCode().equals(results.getCode());
    }
}